package com.base.controller;


import com.base.comm.CommContent;
import com.base.po.voip.*;
import com.base.service.*;
import com.base.utils.IP;
import com.base.utils.Produce;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class add_TourGoods {
    @Autowired
    private LogsService logsService;// log日志
    @Autowired
    private TourGoodsService tourGoodsService;
    @Autowired
    private TourCategoryService tourCategoryService;
    @Autowired
    private UsersService usersService;

    // 账户信息
    @RequestMapping("/add_TourGoods")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        if (adminUserId == null) {
            modelAndView.setViewName("jsp/login");
            return modelAndView;
        }

        Users users = usersService.selectByPrimaryKey(adminUserId);
        List<TourCategory> tourCategory = null;
        if (users.getRole() == 0 || users.getRole() == 1) {
            tourCategory = tourCategoryService.selectByLevel1(adminUserId, adminUserId);
            request.setAttribute("userId", adminUserId);
        }

        request.setAttribute("tourCategory", tourCategory);

        modelAndView.setViewName("jsp/add_TourGoods");
        return modelAndView;
    }


    String SUCCESS = "success";

    // 添加商品
    @RequestMapping(value = "/insertTourGoodsbyjson", method = RequestMethod.POST)
    public void insertTourGoodsbyjson(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject obj)
            throws IOException {
        String goodstext = obj.get("goodstext").toString();
        String showpic = obj.get("showpic").toString();
        String detailpic1 = obj.get("detailpic1").toString();
        String detailpic2 = obj.get("detailpic2").toString();
        String detailpic3 = obj.get("detailpic3").toString();
        String detailpic4 = obj.get("detailpic4").toString();
        String firstclassify = obj.get("firstclassify").toString();
        BigDecimal price=new BigDecimal(Double.parseDouble(obj.get("price").toString()));
        String introduce = obj.get("introduce").toString();
        String context = obj.get("context").toString();
        context=URLDecoder.decode(context,"UTF-8");
        // 返回参数
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String adminId = (String) request.getSession().getAttribute("adminUserId");
        if (adminId == null) {
            out.print("请刷新页面重试！");
            return;
        }

        Users users = usersService.selectByPrimaryKey(adminId);
        List<TourCategory> tourCategory = null;
        if (users.getRole() == 0 || users.getRole() == 1) {
            tourCategory = tourCategoryService.selectByLevel1(adminId, adminId);
        }
        if (tourCategory == null || tourCategory.size() < 1) {
            out.print("商城未添加分类，无法添加商品");
            return;
        }
//        if (context != null && context != "undefined" && !context.equals("")) {
//            if (context.length() > DatabaseUtil.getFieldCapacity()) {
//                out.print("商品详情添加的图片过大");
//                return;
//            }
//        }

        if (showpic == "" || showpic == "undefined") {
            showpic = null;
        }
        if (detailpic1 == "" || detailpic1 == "undefined") {
            detailpic1 = null;
        }
        if (detailpic2 == "" || detailpic2 == "undefined") {
            detailpic2 = null;
        }
        if (detailpic3 == "" || detailpic3 == "undefined") {
            detailpic3 = null;
        }
        if (detailpic4 == "" || detailpic4 == "undefined") {
            detailpic4 = null;
        }

        if (firstclassify != null && !firstclassify.equals("undefined") && !firstclassify.equals("0")) {
            TourCategory firstCategory = tourCategoryService.selectByPrimaryKey(firstclassify);
            if (firstCategory != null) {
                firstclassify = firstCategory.getId();
            } else {
                out.print("未找到对应一级分类");
                return;
            }
        } else {
            firstclassify = null;
        }

        String id = Produce.getUUID();
        int resultCode = 0;
        if (users.getRole() == 0 || users.getRole() == 1) {
            resultCode = tourGoodsService.insertSelective(new TourGoods(id, adminId, adminId,
                    goodstext, showpic, detailpic1, detailpic2, detailpic3, detailpic4,
                    firstclassify, price, introduce, context, (byte) 1, Produce.getNowDate(),
                    Produce.getNowDate(), (byte) 1));
        }

        String usernamesys = (String) request.getSession().getAttribute("useracc");
        if (resultCode == 1) {
            logsService.insert(CommContent.TYPE_UPDATE, "添加" + goodstext + "旅游项目", usernamesys,
                    true, IP.getIpAddr(request));
            out.print(SUCCESS);
        } else {
            logsService.insert(CommContent.TYPE_UPDATE, "添加" + goodstext + "旅游项目", usernamesys,
                    true, IP.getIpAddr(request));
            out.print("更改失败，请您重试");
        }
    }
}
